<?php

class CategorySet
{
	private $categories;
	
	private $firstId;
	private $lastId;
	
	// Constructor
	function __construct($categories = false)
	{
		$this->categories = array();
		
		if(is_array($categories))
			$this->setCategories($categories);
	}
	
	##########################
	# Appends (= adds at the end) an instance of category
	# Use : append($category)
	# Param : category object
	# Returns :
	public function append($category)
	{
		$this->categories[] = $category;
		
		// Update last id
		$this->setLastId($category->getId());
		
		// Update first id is this category is the first to be added
		if($this->count() == 1)
			$this->setFirstId($category->getId());
	}
	
	##########################
	# Prepends (= adds at the beginning) an instance of category
	# Use : prepend($category)
	# Param : category object
	# Returns :
	public function prepend($category) 
	{
		array_unshift($category,$this->categories);
		
		// Update first id
		$this->setFirstId($category->getId());
		
		// Update last id is this category is the first to be added
		if($this->count() == 1)
			$this->setLastId($category->getId());
	}
	
	##########################
	# Gets the range of id of the categories
	# Use : $array = $categorieset->getRange(); $first_id = $array[0]; $last_id = $array[1];
	# Param :
	# Returns : array
	public function getRange()
	{
		$range = array();
		
		if($this->getLastId() > $this->getFirstId())
		{
			$range[0] = $this->getFirstId();
			$range[1] = $this->getLastId();
		}
		else
		{
			$range[0] = $this->getLastId();
			$range[1] = $this->getFirstId();
		}
		
		return $range;
	}
	
	##########################
	# Gets the id of the FIRST category, whatever the sorting is (ASC or DESC)
	# Use : $first_id = $categorieset->getAbsoluteFirstId();
	# Param :
	# Returns : int
	public function getAbsoluteFirstId()
	{
		$range = $this->getRange();
		return $range[0];
	}
	
	##########################
	# Gets the id of the LAST category, whatever the sorting is (ASC or DESC)
	# Use : $last_id = $categorieset->getAbsoluteLastId();
	# Param :
	# Returns : int
	public function getAbsoluteLastId()
	{
		$range = $this->getRange();
		return $range[1];
	}
	
	
	##########################
	# Setters
	
	public function setCategories($categories)
	{
		if(!is_array($categories))
		{
			throw new Exception('Argument must be an array of category instances');
			return;
		}

		$this->categories = $categories;
				
		// Set first and last id
		$this->setFirstId($this->categories[0]->getId());
		$this->setLastId($this->categories[$this->count()-1]->getId());
	}
	
	public function setLastId($id)
	{
		if(is_numeric($id))
			return $this->lastId = $id;
		else
			return false;
	}
	
	public function setFirstId($id)
	{
		if(is_numeric($id))
			return $this->firstId = $id;
		else
			return false;
	}
	
	##########################
	# Getters
	
	public function getCategories()
	{
		return $this->categories;
	}
	
	public function getFirstId()
	{
		return $this->firstId;
	}
	
	public function getLastId()
	{
		return $this->lastId;
	}
	
	public function count()
	{
		return count($this->categories);
	}
	
}

?>